package mapper;

import java.io.IOException;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class MaxTemperatureMapper extends Mapper<LongWritable, Text, Text, IntWritable>{

	private final static Log log = LogFactory.getLog(MaxTemperatureMapper.class);
	
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		
		//模拟操作
		Thread.sleep(100);
		
		String line = value.toString();
		String year = line.substring(15, 19);
		String temp = line.substring(87, 92);
		int airTemperature = NumberUtils.toInt(temp);
		if(airTemperature >= 999){
			
			airTemperature = Integer.MIN_VALUE;
		}
		log.info(String.format("mapper,key:%s,value:%d \n", year, airTemperature));
		context.write(new Text(year), new IntWritable(airTemperature));
	}
}
